package com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.javasm.mb_store_api_final.sys_control.usercontrol.sys_user.entity.SysUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * (SysUser)表数据库访问层
 *
 * @author makejava
 * @since 2024-07-29 20:19:15
 */
public interface SysUserDao extends BaseMapper<SysUser> {

    @Select("SELECT user2.*, depart.depart_name, role.role_name " +
            "FROM (SELECT `user`.*, pos.pos_name " +
            "FROM sys_user user LEFT JOIN sys_pos pos " +
            "ON user.pos_id = pos.pos_id) AS user2 " +
            "LEFT JOIN sys_depart depart " +
            "ON user2.depart_id = depart.depart_id " +
            "LEFT JOIN sys_role role " +
            "ON user2.role_id = role.role_id "
            + "ORDER BY user2.user_id ASC")
    List<SysUser> selectAll();
    @Select("<script>"
            + "SELECT user2.*, depart.depart_name "
            + "FROM (SELECT `user`.*, pos.pos_name "
            + "       FROM sys_user user LEFT JOIN sys_pos pos "
            + "       ON user.pos_id = pos.pos_id) AS user2 "
            + "LEFT JOIN sys_depart depart ON user2.depart_id = depart.depart_id "
            + "LEFT JOIN sys_role role ON user2.role_id = role.role_id "
            + "<if test='departName != null'>"
            + "WHERE depart.depart_name = #{departName} "
            + "</if>"
            + "<if test='userState != null'>"
            + "AND user2.user_state = #{userState} "
            + "</if>"
            + "<if test='posName != null'>"
            + "AND user2.pos_name = #{posName}"
            + "</if>"
            + "<if test='roleName != null'>"
            + "AND role.role_name = #{roleName}"
            + "</if>"
            + "<if test='userName != null'>"
            + "AND user2.user_name LIKE '%${userName}%'"
            + "</if>"
            + "<if test='userAccount != null'>"
            + "AND user2.user_account LIKE '%${userAccount}%'"
            + "</if>"
            + "<if test='userGender != null'>"
            + "AND user2.user_gender = #{userGender}"
            + "</if>"

            + "ORDER BY user2.user_id ASC"
            + "<if test='pageNum != null'>"
            + " LIMIT #{pageNum} ,#{pageSize} "
            + "</if>"
            + "</script>")
    List<SysUser> selectListWithDepart(SysUser user);

    @Insert({
            "INSERT INTO sys_user(user_name, user_password, user_account, user_gender, user_state, user_phone, user_email, depart_id, role_id)",
            "VALUES(#{user.userName},#{user.userPassword}, #{user.userAccount}, #{user.userGender}, #{user.userState}, #{user.userPhone}, #{user.userEmail}, (SELECT depart_id FROM sys_depart WHERE depart_name = #{user.departName}),",
            "(SELECT role_id FROM sys_role WHERE role_name = #{user.roleName}))"
    })
    void add(@Param("user") SysUser user);

    @Update("<script>"
            + "UPDATE sys_user SET "
            + "<if test='user.userName != null'>user_name = #{user.userName} </if>"
            + "<if test='user.userPassword != null'>,user_password = #{user.userPassword} </if>"
            + "<if test='user.userAccount != null'>,user_account = #{user.userAccount}</if>"
            + "<if test='user.userEmail != null'>,user_email = #{user.userEmail} </if>"
            + "<if test='user.userPhone != null'>,user_phone = #{user.userPhone} </if>"
            + "<if test='user.userState != null'>,user_state = #{user.userState} </if>"
            + "<if test='user.userGender != null'>,user_gender = #{user.userGender} </if>"
            + "<if test='user.departId != null'>,depart_id = #{user.departId} </if>"
            + "<if test='user.roleId != null'>,role_id = #{user.roleId} </if>"
            + "<if test='user.remark != null'>,remark = #{user.remark}</if>"
            + "WHERE user_id = #{uid}"
            + "</script>")
    void updateUser(@Param("uid") Integer userId, @Param("user") SysUser user);

    @Delete("DELETE FROM sys_user WHERE user_id = #{userId}")
    void deleteByUid(Integer userId);


    // 新增的方法用于获取记录总数
    @Select("<script>"
            + "SELECT COUNT(*) "
            + "FROM (SELECT `user`.*, pos.pos_name "
            + "       FROM sys_user user LEFT JOIN sys_pos pos "
            + "       ON user.pos_id = pos.pos_id) AS user2 "
            + "LEFT JOIN sys_depart depart ON user2.depart_id = depart.depart_id "
            + "LEFT JOIN sys_role role ON user2.role_id = role.role_id "
            + "<if test='departName != null'>"
            + "WHERE depart.depart_name = #{departName} "
            + "</if>"
            + "<if test='userState != null'>"
            + "AND user2.user_state = #{userState} "
            + "</if>"
            + "<if test='posName != null'>"
            + "AND user2.pos_name = #{posName}"
            + "</if>"
            + "<if test='roleName != null'>"
            + "AND role.role_name = #{roleName}"
            + "</if>"
            + "<if test='userName != null'>"
            + "AND user2.user_name LIKE '%${userName}%'"
            + "</if>"
            + "<if test='userAccount != null'>"
            + "AND user2.user_account LIKE '%${userAccount}%'"
            + "</if>"
            + "<if test='userGender != null'>"
            + "AND user2.user_gender = #{userGender}"
            + "</if>"
            + "</script>")
    Long selectAllCount();
}
